import Mock from 'mockjs';

// 登录接口
Mock.mock('/api/user/login', 'post', (options: { body: string; }) => {
  const { username, password } = JSON.parse(options.body);
  if (username === 'admin' && password === '123456') {
    return {
      code: 200,
      data: {
        token: Mock.Random.guid(),
        username: 'admin',
        role: 'admin',
        lastLogin: Mock.Random.datetime()
      },
      message: '登录成功'
    };
  }
  return {
    code: 401,
    message: '用户名或密码错误'
  };
});

// 注册接口
Mock.mock('/api/v1/user/register', 'post', (options: { body: string; }) => {
  const { username, email } = JSON.parse(options.body);
  return {
    code: 200,
    data: {
      userId: Mock.Random.id(),
      username,
      email,
      role: 'user'
    },
    message: '注册成功'
  };
});

// 注销接口
Mock.mock('/api/v1/user/logout', 'post', () => {
  return {
    code: 200,
    message: '注销成功'
  };
});

// 删除用户接口
Mock.mock(new RegExp('/api/v1/user/.*'), 'delete', () => {
  return {
    code: 200,
    message: '删除成功'
  };
});

// 修改密码接口
Mock.mock('/api/v1/user/password', 'put', (options: { body: string; }) => {
  const { oldPassword, newPassword } = JSON.parse(options.body);
  if (oldPassword === '123456') {
    return {
      code: 200,
      message: '密码修改成功'
    };
  }
  return {
    code: 400,
    message: '原密码错误'
  };
});

// 修改邮箱接口
Mock.mock('/api/v1/user/email', 'put', () => {
  return {
    code: 200,
    message: '邮箱修改成功'
  };
});

// 获取用户信息接口
Mock.mock('/api/user/info', 'get', () => {
  return {
    code: 200,
    data: {
      userId: Mock.Random.id(),
      username: 'admin',
      email: Mock.Random.email(),
      role: 'admin',
      lastLogin: Mock.Random.datetime(),
      createTime: Mock.Random.datetime()
    },
    message: 'success'
  };
});

// 模拟错误响应的拦截器
Mock.setup({
  timeout: '200-600' // 设置响应时间在 200-600ms 之间
});